package com.example.vendingmachine.controller;

import com.example.vendingmachine.dto.SlotDTO;
import com.example.vendingmachine.dto.SlotQuery;
import com.example.vendingmachine.entity.Slot;
import com.example.vendingmachine.service.SlotService;
import com.example.vendingmachine.common.Result;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 货道管理控制器
 */
@RestController
@RequestMapping("/api/slots")
@RequiredArgsConstructor
public class SlotController {

    private final SlotService slotService;

    /**
     * 分页查询货道列表
     */
    @GetMapping
    @PreAuthorize("hasAuthority('slot:list')")
    public Result<PageInfo<Slot>> list(SlotQuery query) {
        return Result.success(slotService.listSlots(query));
    }

    /**
     * 获取货道详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('slot:list')")
    public Result<Slot> getInfo(@PathVariable Long id) {
        return Result.success(slotService.getSlotById(id));
    }

    /**
     * 新增货道
     */
    @PostMapping
    @PreAuthorize("hasAuthority('slot:add')")
    public Result<Long> add(@Validated @RequestBody SlotDTO slotDTO) {
        return Result.success(slotService.createSlot(slotDTO));
    }

    /**
     * 更新货道
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('slot:edit')")
    public Result<Void> update(@PathVariable Long id, @Validated @RequestBody SlotDTO slotDTO) {
        slotDTO.setId(id);
        slotService.updateSlot(slotDTO);
        return Result.success();
    }

    /**
     * 删除货道
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('slot:delete')")
    public Result<Void> delete(@PathVariable Long id) {
        slotService.deleteSlot(id);
        return Result.success();
    }

    /**
     * 更新货道状态
     */
    @PutMapping("/{id}/status")
    @PreAuthorize("hasAuthority('slot:edit')")
    public Result<Void> updateStatus(@PathVariable Long id, @RequestParam String status) {
        slotService.updateSlotStatus(id, status);
        return Result.success();
    }

    /**
     * 更新货道库存
     */
    @PutMapping("/{id}/stock")
    @PreAuthorize("hasAuthority('slot:edit')")
    public Result<Void> updateStock(@PathVariable Long id, @RequestParam Integer stock) {
        slotService.updateSlotStock(id, stock);
        return Result.success();
    }

    /**
     * 检查设备下是否有货道
     */
    @GetMapping("/check-machine")
    @PreAuthorize("hasAuthority('slot:list')")
    public Result<Boolean> hasSlotsByMachineId(@RequestParam Long machineId) {
        return Result.success(slotService.hasSlotsByMachineId(machineId));
    }

    /**
     * 检查商品是否被货道关联
     */
    @GetMapping("/check-product")
    @PreAuthorize("hasAuthority('slot:list')")
    public Result<Boolean> hasSlotsByProductId(@RequestParam Long productId) {
        return Result.success(slotService.hasSlotsByProductId(productId));
    }
} 